<?php

namespace App\Repository;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Cache;
use App\Models\Admin\AdminMenu;


class IndexRepository
{
    /**
     * zTree数据转换
     *
     * @param [type] $category
     * @return void
     */
    static public function tree($category)
    {
        $str = '';
        foreach ($category as $key => $value) {
            if ($value['parent_id'] === 0) {
                $str .= '{id:' . $value['id'] . ', pId:0, name:"' . $value['title'] . '", open:true,chkDisabled:true},';
            } else {
                $str .= '{id:' . $value['id'] . ', pId:' . $value['parent_id'] . ', name:"' . $value['title'] . '", open:true},';
            }
        }
        return $str;
    }

    /**
     * 列表数据转换
     *
     * @return void
     * @author zhaoweichao
     */
    static public function lists($params)
    {
        $lists = AdminMenu::select();
        $records = pagination($lists, $params);
        return $records;
    }

    /**
     * &转换数据格式
     *
     * @param [type] $list
     * @param integer $pid
     * @return void
     */
    static function getTree($list, $pid = 0)
    {
        static $tree = [];
        if (!empty($list)) {

            $newList = [];
            //先修改为以id为下标的列表
            foreach ($list as $k => $v) {
                $newList[$v['id']] = $v;
            }
            //然后开始组装成特殊格式
            foreach ($newList as $value) {
                $value['status'] = true;
                if ($pid == $value['parent_id']) {
                    //先取出顶级
                    $tree[] = &$newList[$value['id']];
                } elseif (isset($newList[$value['parent_id']])) {
                    //再判定非顶级的pid是否存在，如果存在，则再pid所在的数组下面加入一个字段tmp，来将本身存进去
                    $newList[$value['parent_id']]['tmp'][] = &$newList[$value['id']];
                }
            }
        }
        return $tree;
    }

    /**
     * 添加status状态
     *
     * @param [type] $menu
     * @return void
     */
    static function getMenuStatus($menu)
    {
        $name = $_SERVER['REQUEST_URI'];
        $data = [];
        //数据格式化
        foreach ($menu as $k => $v) {
            $data[$v['id']] = $v;
        }
        static $info = [];
        //数据初始渲染
        foreach ($data as $k => $v) {
            $v['status'] = false;
            if ($v['uri'] == $name) {
                $v['status'] = true;
                $parent = $v;
            }
            $info[$k] = $v;
        }
        if (!empty($parent)) {
             //父类数据渲染
            while ($parent['parent_id'] != 0) {
                $pid = $parent['parent_id'];
                if (isset($info[$pid])) {
                    $info[$pid]['status'] = true;
                    $parent = $data[$pid];
                } else {
                    break;
                }
            };
        }
        $info = array_values(array_sort($info, function ($val) {
            return $val['order'];
        }));
        return $info;
    }

    /**
     * 导航数据组成
     *
     * @param [type] $data
     * @return void
     */
    static function getMenu($data)
    {
        //渲染状态
        $data = self::getMenuStatus($data);
        //组成数组树
        $data = self::getTree($data);

        return collect($data);
    }
    
    /**
     * 面包屑组成
     *
     * @param [type] $data
     * @return void
     */
    static public function getCrumbs($data)
    {
        $data = self::getMenuStatus($data);
        static $info = [];
        foreach ($data as $v) {
            if ($v['status'] == true) {
                $info[$v['parent_id']] = $v;
            }
        }
        return $info;
    }
}